//
//  AddressBookFVC.m
//  SelfDriving
//
//  Created by apple on 16/7/2.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AddressBookFVC.h"
#import "BusinessService+WD.h"
#import "SVProgressHUD.h"
#import "LoginResponseModel.h"
#import "InviteDetailsViewController.h"
#import "BusinessService+User.h"
@interface AddressBookFVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table;

@end

@implementation AddressBookFVC
{
    NSMutableArray *data;//添加
    NSMutableArray *rdata;//邀请
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
}

- (void)initData {
    NSMutableDictionary *va = [[NSMutableDictionary alloc]init];
    for (NSMutableDictionary *dic in _ary) {
        if([dic objectForKey:@"phone"]==nil){
            continue;
        }
        [va setObject:[dic objectForKey:@"name"] forKey:[dic objectForKey:@"phone"]==nil?@"":[dic objectForKey:@"phone"]];
        [va setObject:[dic objectForKey:@"img"] forKey:[dic objectForKey:@"name"]==nil?@"":[dic objectForKey:@"name"]];
    }
    data = [[NSMutableArray alloc]init];
    rdata = [[NSMutableArray alloc]init];
    self.navigationItem.title = @"添加通讯录好友";
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:_phone forKey:@"phone"];
    [[BusinessService shareInstance]importContact:dic success:^(id result) {
        NSMutableArray *ary = result;
        for(LoginResponseModel *user in ary){
            if ([user.type intValue]==1){
                NSMutableDictionary *dic= [[NSMutableDictionary alloc]init];
                NSString *name = [va objectForKey:user.phone];
                if (name==nil||name.length == 0) {
                    continue;
                }
                [dic setObject:name forKey:@"name"];
                [dic setObject:[va objectForKey:[va objectForKey:user.phone]] forKey:@"img"];
                [dic setObject:user.phone forKey:@"phone"];
                [dic setObject:user.type forKey:@"type"];
                [dic setObject:user.uid forKey:@"uid"];
                [data addObject:dic];
            }else{
                NSMutableDictionary *dic= [[NSMutableDictionary alloc]init];
                NSString *name = [va objectForKey:user.phone];
                if (name==nil||name.length == 0) {
                    continue;
                }
                [dic setObject:name forKey:@"name"];
                [dic setObject:[va objectForKey:[va objectForKey:user.phone]] forKey:@"img"];
                [dic setObject:user.phone forKey:@"phone"];
                [dic setObject:user.type forKey:@"type"];
                [dic setObject:user.uid forKey:@"uid"];
                [rdata addObject:dic];
            }
        }
        
        [_table reloadData];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return  data.count;
    }
    return rdata.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return [NSString stringWithFormat:@"%lu个好友等待添加",(unsigned long)data.count];
    }
    return [NSString stringWithFormat:@"%lu个好友可邀请",(unsigned long)rdata.count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        NSMutableDictionary *user = [data objectAtIndex:indexPath.row];
        cell.textLabel.text = [user objectForKey:@"name"];
        BaseButton *btn = [[BaseButton alloc]initWithFrame:CGRectMake(SCRE_WIDTH - 45, 45/2-25/2, 35, 25)];
        btn.dic = user;
        [btn setTitle:@"关注" forState:UIControlStateNormal];
        [btn setTitleColor:colorBlack forState:UIControlStateNormal];
        btn.layer.cornerRadius = 4;
        btn.layer.borderColor = colorBG.CGColor;
        btn.layer.borderWidth = 0.8;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn addTarget:self action:@selector(todaction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn];
    }else{
        NSMutableDictionary *user = [rdata objectAtIndex:indexPath.row];
        cell.textLabel.text = [user objectForKey:@"name"];
        BaseButton *btn = [[BaseButton alloc]initWithFrame:CGRectMake(SCRE_WIDTH - 45, 45/2-25/2, 35, 25)];
        btn.dic = user;
        [btn setTitle:@"邀请" forState:UIControlStateNormal];
        [btn setTitleColor:colorBlack forState:UIControlStateNormal];
        btn.layer.cornerRadius = 4;
        btn.layer.borderColor = colorBG.CGColor;
        btn.layer.borderWidth = 0.8;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn addTarget:self action:@selector(todaction:) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView addSubview:btn];
    }
    return cell;
}
#pragma mark 关注/邀请
-(void)todaction:(BaseButton *)btn{
    if([[btn.dic objectForKey:@"type"] intValue] == 1){//关注
         NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[btn.dic objectForKey:@"uid"] forKey:@"fid"];
        [dic setObject:@"0" forKey:@"type"];
        [[BusinessService shareInstance]follow:dic success:^(id result) {
            [SVProgressHUD showSuccessWithStatus:@"关注成功"];
            //xhw2018-1-18加
            [self initData];
         } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
        
    }else{
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                     bundle:[NSBundle mainBundle]];
        InviteDetailsViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"InviteDetailsViewController"];
        searchResultDetailController.dic = btn.dic;
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    }
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
